using System;
using System.Collections.Generic;
using System.Drawing;
using GTA;
using GTA.Native;
using NativeUI;

namespace CustomPeds.Menus;

public class ModelMenu : Script
{
	public static UIMenu menu = new UIMenu("", TextSetting.ms_Menu, new Point(0, -107));
	private static bool isMenuInitialized;

	private static string resumeModel;

	public ModelMenu()
	{
		if (!isMenuInitialized)
		{
			Main.pool.Add(menu);
			menu.OnItemSelect += OnItemSelect;
			menu.OnMenuClose += OnMenuClose;
			isMenuInitialized = true;
		}
	}

	public static void InitModelMenu()
	{
		menu.Clear();
		if (DressingScript.Dressing == Main.Member.P2)
		{
			resumeModel = Main.P2.skin.ModelName;
		}
		else
		{
			resumeModel = Main.P1.skin.ModelName;
		}
		menu.SetBannerType(new UIResRectangle());
		menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_Male));
		menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_Female));
		menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_Player));
		menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_FreeMode));
		menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_Cop));
		menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_LSFD_Paramedic));
		menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_Army));
		menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_AddonPeds));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Randomize));
		menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_Resume));
		menu.AddItem(new UIMenuItem(TextSetting.ms_MenuItem_Find));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Distance));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnLeft));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnRight));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleUp));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleDown));
		for (int i = 0; i <= 7; i++)
		{
			menu.BindMenuToItem(ModelSubMenu.menu, menu.MenuItems[i]);
		}
		menu.RefreshIndex();
	}

	private void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
	{
		List<string> list = new List<string>();
		if (index <= 7)
		{
			ModelSubMenu.InitModelSubMenu(index);
		}
		switch (index)
		{
		case 8:
		{
			Random random = new Random(Guid.NewGuid().GetHashCode());
			list = new List<string>(Enum.GetNames(typeof(PedHash)));
			if (DressingScript.Dressing == Main.Member.P2)
			{
				Main.ReBuildP2(Main.P2.Character, list[random.Next(0, list.Count)]);
			}
			else
			{
				Main.ReBuildClone(Main.Clone.Character, list[random.Next(0, list.Count)]);
			}
			break;
		}
		case 9:
			if (DressingScript.Dressing == Main.Member.P2)
			{
				Main.ReBuildP2(Main.P2.Character, resumeModel);
			}
			else
			{
				Main.ReBuildClone(Main.Clone.Character, resumeModel);
			}
			break;
		case 10:
		{
			string findText = Game.GetUserInput(string.Empty, 30);
			int num = -1;
			list = new List<string>(Enum.GetNames(typeof(PedHash)));
			num = list.FindIndex(name => string.Compare(name, findText, StringComparison.OrdinalIgnoreCase) == 0 && name.Length == findText.Length);
			if (num != -1)
			{
				if (DressingScript.Dressing == Main.Member.P2)
				{
					Main.ReBuildP2(Main.P2.Character, list[num]);
				}
				else
				{
					Main.ReBuildClone(Main.Clone.Character, list[num]);
				}
			}
			break;
		}
		case 11:
			DressingScript.Distance();
			break;
		case 12:
			DressingScript.TurnLeft();
			break;
		case 13:
			DressingScript.TurnRight();
			break;
		case 14:
			DressingScript.AngleUp();
			break;
		case 15:
			DressingScript.AngleDown();
			break;
		}
	}

	private void OnIndexChange(UIMenu sender, int index)
	{
	}

	private void UpdateModel()
	{
		if (DressingScript.Dressing == Main.Member.P2)
		{
			Main.P2.skin.ModelName = Origin.GetPedModelName(Main.P2.Character.Model);
			Skin.TryConvertAddonPedName(ref Main.P2.skin.ModelName);
		}
		else
		{
			Main.Clone.skin.ModelName = Origin.GetPedModelName(Main.Clone.Character.Model);
			Skin.TryConvertAddonPedName(ref Main.Clone.skin.ModelName);
			Main.P1.skin.ModelName = Main.Clone.skin.ModelName;
		}
	}

	private void OnMenuClose(UIMenu sender)
	{
		menu.Visible = false;
		menu.RefreshIndex();
		if (DressingScript.Dressing == Main.Member.P1 && resumeModel != Main.Clone.skin.ModelName)
		{
			bool num = Main.ChangePlayerModel(Origin.GetPedModelName(Main.Clone.Character.Model));
			bool isAddonPed = Main.Clone.skin.IsAddonPed;
			isAddonPed = ((!num || isAddonPed) ? true : false);
			if (!num)
			{
				Main.ChangePlayerModel("Franklin");
			}
			Main.P1.BuildPedData(Game.Player.Character, Main.Member.P1, isAddonPed);
			Game.Player.Character.SetDefaultClothes();
			Game.Player.Character.Task.PlayAnimation("missheistdocks2bleadinoutlsdh_2b_int", "standing_loop_wade", 8f, -4f, -1, AnimationFlags.StayInEndFrame, 1f);
		}
		UpdateModel();
		DressingScript.ReDressing(DressingScript.Dressing);
	}
}
